#################################################################################################################
###### The following code is to reproduce the analysis in ``Did Georgian Voters Desire Military Escalation ######  
###### in 2008? Experiments \& Observation''.  Note, the data for the second round of the survey is        ######
###### hosted on the CRRC website.  We will update the link to those data periodically, should the current ######
###### location change.                                                                                    ######
#################################################################################################################



rm(list=ls())
library(scales)
library(readstata13)
library(foreign)
library(ggplot2)
library(gtable)
library(grid)
library(xtable)

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=T,
                      conf.interval=.95, .drop=TRUE) {
  require(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}

setEPS()
##################################
### Set working directory below###
##################################

setwd(##########)

first.wave<-read.dta("first_wave.dta")
first.wave$dv_rs<-rescale(first.wave$dv, to= c(0,3), from = range(first.wave$dv, na.rm=T))
  
#########################################
######### Figure 2        ###############
#########################################


dvc.5c <- summarySE(first.wave, measurevar="dv_rs", groupvars=c("treat"))
dvc.6c <- dvc.5c
dvc.6c$treat <- factor(dvc.6c$treat)

figure.2.table<-xtable(dvc.6c)
figure.2.table$lo<-figure.2.table$dv_rs-figure.2.table$ci
figure.2.table$hi<-figure.2.table$dv_rs+figure.2.table$ci
figure.2.table<-xtable(figure.2.table[ , c(1,2,3,5,7,8)])
digits(figure.2.table)[c(2,3)] <- 0

print(figure.2.table,include.rownames=FALSE)

postscript("all_pre_conflict_no_region.eps")

ggplot(dvc.6c, aes(x=treat, y=dv_rs, fill=treat)) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           size=.3) +      
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    
                width=.2,
                position=position_dodge(.9)) +
  xlab("Treatment") +
  ylab("Support for President") +
  scale_fill_grey(name="Vignette", 
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose")) +
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  theme_bw()+ 
  theme(legend.justification=c(0,1), legend.position=c(0,1))+  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +  theme(axis.line = element_line(color = 'black'))
dev.off()


##########################################
### DATA ON THE DIFFERENT CROSSTABS ######
###           Figure 3            ########
##########################################

myvars <- c("dv_rs","treat","pol_dir")
first.wave.a<-first.wave[myvars]
first.wave.a <- na.omit(first.wave.a)
first.wave.a$pol_dir[first.wave.a$pol_dir==5] <- 7
first.wave.a$pol_dir[first.wave.a$pol_dir==1] <- 5
first.wave.a$pol_dir[first.wave.a$pol_dir==2] <- 5
first.wave.a$pol_dir[first.wave.a$pol_dir==3] <- 6
first.wave.a$pol_dir[first.wave.a$pol_dir==4] <- 7
dvc.5a <- summarySE(first.wave.a, measurevar="dv_rs", groupvars=c("treat","pol_dir"))
dvc.6a <- dvc.5a
dvc.6a$pol_dir <- factor(dvc.6a$pol_dir)
dvc.6a$treat <- factor(dvc.6a$treat)
names(dvc.6a) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")

figure.3.poldir.table<-xtable(dvc.6a)
figure.3.poldir.table$lo<-figure.3.poldir.table$dv_rs-figure.3.poldir.table$ci
figure.3.poldir.table$hi<-figure.3.poldir.table$dv_rs+figure.3.poldir.table$ci
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==7] <- "Right Direction"
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==6] <- "Neither"
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==5] <- "Wrong Direction"
caption.3.poldir<-c("Figure 3: What direction is the country going")
figure.3.poldir.table<-xtable(figure.3.poldir.table[ , c(1,10,3,4,6,8,9)], caption.3.poldir)
digits(figure.3.poldir.table)[c(2,3,4)] <- 0

print(figure.3.poldir.table,include.rownames=FALSE)


myvars <- c("dv_rs","treat","highed")
first.wave.b<-first.wave[myvars]
first.wave.b <- na.omit(first.wave.b)
first.wave.b$highed[first.wave.b$highed==1] <- 2
first.wave.b$highed[first.wave.b$highed==0] <- 1
dvc.5b <- summarySE(first.wave.b, measurevar="dv_rs", groupvars=c("treat","highed"))
dvc.6b <- dvc.5b
dvc.6b$highed <- factor(dvc.6b$highed)
dvc.6b$treat <- factor(dvc.6b$treat)
names(dvc.6b) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")

figure.3.ed.table<-xtable(dvc.6b)
figure.3.ed.table$lo<-figure.3.ed.table$dv_rs-figure.3.ed.table$ci
figure.3.ed.table$hi<-figure.3.ed.table$dv_rs+figure.3.ed.table$ci
figure.3.ed.table$ed[figure.3.ed.table$var==1] <- "Low"
figure.3.ed.table$ed[figure.3.ed.table$var==2] <- "High"
caption.3.ed<-c("Figure 3: High or Low Educational Attainment")
figure.3.ed.table<-xtable(figure.3.ed.table[ , c(1,10,3,4,6,8,9)], caption.3.ed)
digits(figure.3.ed.table)[c(2,3,4)] <- 0
print(figure.3.ed.table,include.rownames=FALSE)


myvars <- c("dv_rs","treat","highvote")
first.wave.c<-first.wave[myvars]
first.wave.c <- na.omit(first.wave.c)
first.wave.c$highvote[first.wave.c$highvote==1] <- 4
first.wave.c$highvote[first.wave.c$highvote==0] <- 3
dvc.5c <- summarySE(first.wave.c, measurevar="dv_rs", groupvars=c("treat","highvote"))
dvc.6c <- dvc.5c
dvc.6c$highvote <- factor(dvc.6c$highvote)
dvc.6c$treat <- factor(dvc.6c$treat)
names(dvc.6c) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")

figure.3.vote.table<-xtable(dvc.6c)
figure.3.vote.table$lo<-figure.3.vote.table$dv_rs-figure.3.vote.table$ci
figure.3.vote.table$hi<-figure.3.vote.table$dv_rs+figure.3.vote.table$ci
figure.3.vote.table$vote[figure.3.vote.table$var==3] <- "Low"
figure.3.vote.table$vote[figure.3.vote.table$var==4] <- "High"
caption.3.vote<-c("Figure 3: High or Low Likelihood to Vote")
figure.3.vote.table<-xtable(figure.3.vote.table[ , c(1,10,3,4,6,8,9)], caption.3.vote)
digits(figure.3.vote.table)[c(2,3,4)] <- 0
print(figure.3.vote.table,include.rownames=FALSE)



dvc.6d <- rbind(dvc.6b,dvc.6c,dvc.6a)

postscript("pre_war_bar_chart_subgroups.eps")

ggplot(dvc.6d, aes(x=var, y=dv_rs, fill=treat)) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black",
           size=.3) +      
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(.9)) +
  xlab("Education, Likelihood of Voting, and Direction of Politics in Georgia") +
  ylab("Support for President") +
  scale_fill_grey(name="Treatment", 
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose")) +
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  scale_x_discrete(labels=c("Low Ed", "High Ed", "Unlikely \n Voter", "Likely \n Voter", 
                            "Wrong \n Direction", "Neither", "Right \n Direction"))+
  theme_bw()+
  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))
dev.off()


#######################################################
#########  Figure 4                  ##################
#######################################################


sec.wave<-read.dta("http://caucasusbarometer.org/downloads/Postconflict_2008_Only_responses_20112012.dta")
sec.wave$dv_rs<-rescale(sec.wave$dv, to= c(0,3), from = range(sec.wave$dv, na.rm=T))



dvc.12.1<-summarySE(sec.wave, measurevar="Q21_1")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.2<-summarySE(sec.wave, measurevar="Q21_2")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.3<-summarySE(sec.wave, measurevar="Q21_3")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.4<-summarySE(sec.wave, measurevar="Q21_4")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.5<-summarySE(sec.wave, measurevar="Q21_5")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.6<-summarySE(sec.wave, measurevar="Q21_6")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.7<-summarySE(sec.wave, measurevar="Q21_7")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.8<-summarySE(sec.wave, measurevar="Q21_8")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.9<-summarySE(sec.wave, measurevar="Q21_9")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.10<-summarySE(sec.wave, measurevar="Q21_10")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.11<-summarySE(sec.wave, measurevar="Q21_11")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.12<-summarySE(sec.wave, measurevar="Q21_12")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.13<-summarySE(sec.wave, measurevar="Q21_13")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.14<-summarySE(sec.wave, measurevar="Q21_14")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.15<-summarySE(sec.wave, measurevar="Q21_15")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.16<-summarySE(sec.wave, measurevar="Q21_16")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.17<-summarySE(sec.wave, measurevar="Q21_17")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.18<-summarySE(sec.wave, measurevar="Q21_18")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.19<-summarySE(sec.wave, measurevar="Q21_19")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.20<-summarySE(sec.wave, measurevar="Q21_20")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.21<-summarySE(sec.wave, measurevar="Q21_21")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.22<-summarySE(sec.wave, measurevar="Q21_22")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.23<-summarySE(sec.wave, measurevar="Q21_23")#, groupvars=c("treatment","confirmed_conflict"))
dvc.12.24<-summarySE(sec.wave, measurevar="Q21_24")#, groupvars=c("treatment","confirmed_conflict"))

dvc.12.1$.id<-1
dvc.12.2$.id<-2
dvc.12.3$.id<-3
dvc.12.1$prob<- "Causes"
dvc.12.2$prob<- "Causes"
dvc.12.3$prob<- "Causes"
dvc.12.4$.id<-4
dvc.12.5$.id<-5
dvc.12.6$.id<-6
dvc.12.7$.id<-7
dvc.12.8$.id<-8
dvc.12.4$prob<- "Who Started"
dvc.12.5$prob<- "Who Started"
dvc.12.6$prob<- "Who Started"
dvc.12.7$prob<- "Who Started"
dvc.12.8$prob<- "Who Started"
dvc.12.9$.id<-9
dvc.12.10$.id<-10
dvc.12.11$.id<-11
dvc.12.12$.id<-12
dvc.12.13$.id<-13
dvc.12.14$.id<-14
dvc.12.15$.id<-15
dvc.12.16$.id<-16
dvc.12.17$.id<-17
dvc.12.9$prob<- "Performance"
dvc.12.10$prob<- "Performance"
dvc.12.11$prob<- "Prosecution"
dvc.12.12$prob<- "Prosecution"
dvc.12.13$prob<- "Performance"
dvc.12.14$prob<- "Prosecution"
dvc.12.15$prob<- "Prosecution"
dvc.12.16$prob<- "Prosecution"
dvc.12.17$prob<- "Prosecution"

dvc.12.18$.id<-18
dvc.12.19$.id<-19
dvc.12.18$prob<- "The West"
dvc.12.19$prob<- "The West"

dvc.12.20$.id<-20 
dvc.12.20$prob<- "Performance"

dvc.12.21$.id<-21
dvc.12.21$prob<- "Prosecution"

dvc.12.22$.id<-22
dvc.12.23$.id<-23
dvc.12.24$.id<-24
dvc.12.22$prob<- "Unsure"
dvc.12.23$prob<- "Unsure"
dvc.12.24$prob<- "Unsure"

names(dvc.12.1) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.2) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.3) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.4) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.5) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.6) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.7) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.8) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.9) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.10) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.11) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.12) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.13) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.14) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.15) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.16) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.17) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.18) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.19) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.20) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.21) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.22) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.23) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")
names(dvc.12.24) <- c(".id","N", "Q21", "sd", "se", "ci", "prob")

dvc.12<-rbind(dvc.12.1,dvc.12.2,dvc.12.3,dvc.12.4,dvc.12.5,dvc.12.6,dvc.12.7,dvc.12.8,dvc.12.9,dvc.12.10,
              dvc.12.11,dvc.12.12,dvc.12.13,dvc.12.14,dvc.12.15,dvc.12.16,dvc.12.17,dvc.12.18,dvc.12.19,dvc.12.20,
              dvc.12.21,dvc.12.22,dvc.12.23, dvc.12.24)
dvc.12$labels <-c("Ancient times", "Soviet times", "Independence", "Rus told Militias to Attack",
                  "S.O. militias attacked first", "Geo attacked first", 
                  "Rus attacked first", "No one knows who attacked first", 
                  "Geo military did well", " Geo military did poorly", 
                  "Rus targeted civilians", "Geo targeted civilians", 
                  "Rus attacked Tbilisi", "Rus sent troops into Abkhazia",
                  "Rus attacked troops in Abkhazia", "Rus sent troops into Adjara", 
                  "Rus attacked troops in Adjara", "West didn't come to aid", 
                  "West pressured Rus to moderate", 
                  "Rus occupied terr, won't leave", "Many Geo killed or murdered",
                  "We really don't know", "Don't know, Geo media lies", 
                  "Don't know, Rus media lies")

dvc.12$.id <- factor(dvc.12$.id)


postscript("narratives.eps")

ggplot(dvc.12, aes(x=labels, y=Q21))+
  geom_point(aes(shape=prob), size = 3)+
  scale_x_discrete(limits=c("We really don't know", "Don't know, Geo media lies","Don't know, Rus media lies", "West pressured Rus to moderate", "West didn't come to aid","Many Geo killed or murdered","Rus attacked troops in Adjara","Rus sent troops into Adjara", "Rus attacked troops in Abkhazia","Rus sent troops into Abkhazia", "Geo targeted civilians", "Rus targeted civilians","Rus occupied terr, won't leave", "Rus attacked Tbilisi", " Geo military did poorly", "Geo military did well", "No one knows who attacked first", "Rus attacked first","Geo attacked first", "S.O. militias attacked first","Rus told Militias to Attack", "Independence", "Soviet times", "Ancient times"
  ))+
  geom_errorbar(aes(ymin=Q21-ci, ymax=Q21+ci),
                size=.7,    
                width=.2,
                position=position_dodge(.9))+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(axis.text.x  = element_text(hjust=1, vjust=0.7, size=8),axis.text.y  = element_text(size=8))+
  theme(legend.text = element_text(size=8))+
  labs(shape = "Issues",y = "Share of respondents mentioning this event in their narrative", x = "Categories of stories told by respondents")+
  coord_flip()
dev.off()
#######################################################
#########  Figure 5                  ##################
#######################################################

dvc.9a<-summarySE(first.wave, measurevar="dv_rs", groupvars=c("treat"))
dvc.9a$.id<-1
dvc.10a<-summarySE(sec.wave, measurevar="dv_rs")
dvc.10a$.id<-2
dvc.10a$treat<-"Real War"
names(dvc.9a) <- c("treat","N", "dv_rs", "sd", "se", "ci", ".id")
dvc.11a<-rbind(dvc.9a,dvc.10a)
dvc.11a$.id <- factor(dvc.11a$.id)

figure.5.table<-xtable(dvc.11a)
figure.5.table$lo<-figure.5.table$dv_rs-figure.5.table$ci
figure.5.table$hi<-figure.5.table$dv_rs+figure.5.table$ci
caption.5<-c("Figure 5: Experiment versus Actual War")
figure.5.table<-xtable(figure.5.table[ , c(1,2,3,5,8,9)], caption.5)
digits(figure.5.table)[c(2,3)] <- 0
print(figure.5.table,include.rownames=FALSE)


postscript("rally_round_comp.eps")

ggplot(dvc.11a, aes(x=.id, y=dv_rs, fill=treat )) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black",
           size=.3) +      
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    # Thinner lines
                width=.1,
                position=position_dodge(.9)) +
  xlab("Pre- and Post-War Responses") +
  ylab("Support for President") +
  scale_fill_grey(name="Survey", 
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Real War"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Actual War"))+
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  theme_bw()+
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
 
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))


dev.off()

###############################################################
############  Figure 7                        #################
###############################################################



dvc.9b<-summarySE(first.wave, measurevar="dv_rs", groupvars=c("treat","reg_conflict" ))
dvc.9b$.id<-1
dvc.9b$.idfact<-paste(dvc.9b$.id, dvc.9b$reg_conflict)
dvc.10b<-summarySE(sec.wave, measurevar="dv_rs", groupvars=c("treatment","confirmed_conflict"))
dvc.10b$.id<-2
dvc.10b$.idfact<-paste(dvc.10b$.id, dvc.10b$confirmed_conflict)
dvc.10b$treatment[dvc.10b$treatment==0] <- "Init, Lose"
dvc.10b$treatment[dvc.10b$treatment==1] <-"Target, Lose"
dvc.10b$treatment[dvc.10b$treatment==2] <-"Init, Win"
dvc.10b$treatment[dvc.10b$treatment==3] <-"Target, Win"
names(dvc.9b) <- c("treatment","confirmed_conflict","N", "dv_rs", "sd", "se", "ci", ".id", ".idfact")
dvc.11b<-rbind(dvc.9b,dvc.10b)
dvc.11b$.id <- factor(dvc.11b$.id)


figure.7.table<-xtable(dvc.11b)
figure.7.table$lo<-figure.7.table$dv_rs-figure.7.table$ci
figure.7.table$hi<-figure.7.table$dv_rs+figure.7.table$ci
caption.7<-c("Figure 7: Proximity to Conflict ")
figure.7.table<-xtable(figure.7.table[ , c(1,2,3,4,6,10,11)], caption.7)
digits(figure.7.table)[c(2, 3,4 )] <- 0

print(figure.7.table,include.rownames=FALSE)



postscript("rally_round_comp_time_firsta.eps")

ggplot(dvc.11b, aes(x=.idfact, y=dv_rs, fill=treatment )) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black",
           size=.3) +     
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    
                width=.1,
                position=position_dodge(.9)) +
  geom_text(aes(y=-.05,label=N), size=3, 
            position= position_dodge(width=0.9), 
            color="black") +
  xlab("Pre- and Post-War Responses") +
  ylab("Support for President") +
  scale_fill_grey(name="Survey",
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Real War"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Actual War"))+
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(-0.1,3)) +
  theme_bw()+
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ 
  theme(legend.justification=c(0,1), legend.position=c(0,1))+theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))

dev.off()

#####################################################
######Work on excluding ethnic minorities data#######
#####################################################
######Note, these are sampled villages with   #######
######over 1/3 ethnic minority.  Only one case#######
######excluded had under 50%.                 #######
#####################################################


first.wave$ethnic.enclave<-0
first.wave$ethnic.enclave[first.wave$sher==97] <- 1
first.wave$ethnic.enclave[first.wave$sher==99] <- 1
first.wave$ethnic.enclave[first.wave$sher==100] <- 1
first.wave$ethnic.enclave[first.wave$sher==101] <- 1
first.wave$ethnic.enclave[first.wave$sher==109] <- 1
first.wave$ethnic.enclave[first.wave$sher==94] <- 1
first.wave$ethnic.enclave[first.wave$sher==96] <- 1
enclave.excluded<-subset(first.wave, ethnic.enclave==0)


##############################################################
######### Figure 2        ####################################
##############################################################


dvc.5c <- summarySE(enclave.excluded, measurevar="dv_rs", groupvars=c("treat"))
dvc.6c <- dvc.5c
#dvc.6c$reg_conflict <- factor(dvc.6c$reg_conflict)
dvc.6c$treat <- factor(dvc.6c$treat)

figure.2.table<-xtable(dvc.6c)
figure.2.table$lo<-figure.2.table$dv_rs-figure.2.table$ci
figure.2.table$hi<-figure.2.table$dv_rs+figure.2.table$ci
figure.2.table<-xtable(figure.2.table[ , c(1,2,3,5,7,8)])
digits(figure.2.table)[c(2,3)] <- 0

print(figure.2.table,include.rownames=FALSE)

postscript("figure_2_no_ethn.eps")

ggplot(dvc.6c, aes(x=treat, y=dv_rs, fill=treat)) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", # Use black outlines,
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(.9)) +
  xlab("Treatment") +
  ylab("Support for President") +
  scale_fill_grey(name="Vignette", # Legend label, use darker colors
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose")) +
  #  ggtitle("Initiation is Rewarded if the War Succeeds") +
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  theme_bw()+ 
  theme(legend.justification=c(0,1), legend.position=c(0,1))+  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +  theme(axis.line = element_line(color = 'black'))
dev.off()


##########################################
### DATA ON THE DIFFERENT CROSSTABS ######
###           Figure 3            ########
##########################################

myvars <- c("dv_rs","treat","pol_dir")
first.wave.a<-enclave.excluded[myvars]
first.wave.a <- na.omit(first.wave.a)
first.wave.a$pol_dir[first.wave.a$pol_dir==5] <- 7
first.wave.a$pol_dir[first.wave.a$pol_dir==1] <- 5
first.wave.a$pol_dir[first.wave.a$pol_dir==2] <- 5
first.wave.a$pol_dir[first.wave.a$pol_dir==3] <- 6
first.wave.a$pol_dir[first.wave.a$pol_dir==4] <- 7
dvc.5a <- summarySE(first.wave.a, measurevar="dv_rs", groupvars=c("treat","pol_dir"))
dvc.6a <- dvc.5a
dvc.6a$pol_dir <- factor(dvc.6a$pol_dir)
dvc.6a$treat <- factor(dvc.6a$treat)
names(dvc.6a) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")

figure.3.poldir.table<-xtable(dvc.6a)
figure.3.poldir.table$lo<-figure.3.poldir.table$dv_rs-figure.3.poldir.table$ci
figure.3.poldir.table$hi<-figure.3.poldir.table$dv_rs+figure.3.poldir.table$ci
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==7] <- "Right Direction"
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==6] <- "Neither"
figure.3.poldir.table$pol_dir[figure.3.poldir.table$var==5] <- "Wrong Direction"
caption.3.poldir<-c("Figure 3: What direction is the country going")
figure.3.poldir.table<-xtable(figure.3.poldir.table[ , c(1,10,3,4,6,8,9)], caption.3.poldir)
digits(figure.3.poldir.table)[c(2,3,4)] <- 0

print(figure.3.poldir.table,include.rownames=FALSE)


myvars <- c("dv_rs","treat","highed")
first.wave.b<-enclave.excluded[myvars]
first.wave.b <- na.omit(first.wave.b)
first.wave.b$highed[first.wave.b$highed==1] <- 2
first.wave.b$highed[first.wave.b$highed==0] <- 1
dvc.5b <- summarySE(first.wave.b, measurevar="dv_rs", groupvars=c("treat","highed"))
dvc.6b <- dvc.5b
dvc.6b$highed <- factor(dvc.6b$highed)
dvc.6b$treat <- factor(dvc.6b$treat)
names(dvc.6b) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")


figure.3.ed.table<-xtable(dvc.6b)
figure.3.ed.table$lo<-figure.3.ed.table$dv_rs-figure.3.ed.table$ci
figure.3.ed.table$hi<-figure.3.ed.table$dv_rs+figure.3.ed.table$ci
figure.3.ed.table$ed[figure.3.ed.table$var==1] <- "Low"
figure.3.ed.table$ed[figure.3.ed.table$var==2] <- "High"
caption.3.ed<-c("Figure 3: High or Low Educational Attainment")
figure.3.ed.table<-xtable(figure.3.ed.table[ , c(1,10,3,4,6,8,9)], caption.3.ed)
digits(figure.3.ed.table)[c(2,3,4)] <- 0
print(figure.3.ed.table,include.rownames=FALSE)


myvars <- c("dv_rs","treat","highvote")
first.wave.c<-enclave.excluded[myvars]
first.wave.c <- na.omit(first.wave.c)
first.wave.c$highvote[first.wave.c$highvote==1] <- 4
first.wave.c$highvote[first.wave.c$highvote==0] <- 3
dvc.5c <- summarySE(first.wave.c, measurevar="dv_rs", groupvars=c("treat","highvote"))
dvc.6c <- dvc.5c
dvc.6c$highvote <- factor(dvc.6c$highvote)
dvc.6c$treat <- factor(dvc.6c$treat)
names(dvc.6c) <- c("treat", "var","N", "dv_rs", "sd", "se", "ci")

figure.3.vote.table<-xtable(dvc.6c)
figure.3.vote.table$lo<-figure.3.vote.table$dv_rs-figure.3.vote.table$ci
figure.3.vote.table$hi<-figure.3.vote.table$dv_rs+figure.3.vote.table$ci
figure.3.vote.table$vote[figure.3.vote.table$var==3] <- "Low"
figure.3.vote.table$vote[figure.3.vote.table$var==4] <- "High"
caption.3.vote<-c("Figure 3: High or Low Likelihood to Vote")
figure.3.vote.table<-xtable(figure.3.vote.table[ , c(1,10,3,4,6,8,9)], caption.3.vote)
digits(figure.3.vote.table)[c(2,3,4)] <- 0
print(figure.3.vote.table,include.rownames=FALSE)





dvc.6d <- rbind(dvc.6b,dvc.6c,dvc.6a)






postscript("figure_3_no_ethn.eps")

ggplot(dvc.6d, aes(x=var, y=dv_rs, fill=treat)) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           size=.3) +      
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    
                width=.2,
                position=position_dodge(.9)) +
  xlab("Education, Likelihood of Voting, and Direction of Politics in Georgia") +
  ylab("Support for President") +
  scale_fill_grey(name="Treatment", # Legend label, use darker colors
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose")) +
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  scale_x_discrete(labels=c("Low Ed", "High Ed", "Unlikely \n Voter", "Likely \n Voter", 
                            "Wrong \n Direction", "Neither", "Right \n Direction"))+
  theme_bw()+
  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))
dev.off()



#######################################################
#########  Figure 5                  ##################
#######################################################

#first.wave$dv_rs<-rescale(first.wave$dv, to= c(1,4), from = range(first.wave$dv, na.rm=T))
dvc.9a<-summarySE(enclave.excluded, measurevar="dv_rs", groupvars=c("treat"))
dvc.9a$.id<-1
dvc.10a<-summarySE(sec.wave, measurevar="dv_rs")
dvc.10a$.id<-2
dvc.10a$treat<-"Real War"
names(dvc.9a) <- c("treat","N", "dv_rs", "sd", "se", "ci", ".id")
dvc.11a<-rbind(dvc.9a,dvc.10a)
dvc.11a$.id <- factor(dvc.11a$.id)

figure.5.table<-xtable(dvc.11a)
figure.5.table$lo<-figure.5.table$dv_rs-figure.5.table$ci
figure.5.table$hi<-figure.5.table$dv_rs+figure.5.table$ci
caption.5<-c("Figure 5: Experiment versus Actual War")
figure.5.table<-xtable(figure.5.table[ , c(1,2,3,5,8,9)], caption.5)
digits(figure.5.table)[c(2,3)] <- 0
print(figure.5.table,include.rownames=FALSE)


postscript("figure_5_no_ethn.eps")

ggplot(dvc.11a, aes(x=.id, y=dv_rs, fill=treat )) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", # Use black outlines,
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    # Thinner lines
                width=.1,
                position=position_dodge(.9)) +
  xlab("Pre- and Post-War Responses") +
  ylab("Support for President") +
  scale_fill_grey(name="Survey", # Legend label, use darker colors
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Real War"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Actual War"))+
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(0,3)) +
  theme_bw()+
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
  #draws x and y axis line
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))


dev.off()

###############################################################
############  Figure 7                        #################
###############################################################



dvc.9b<-summarySE(enclave.excluded, measurevar="dv_rs", groupvars=c("treat","reg_conflict" ))
dvc.9b$.id<-1
dvc.9b$.idfact<-paste(dvc.9b$.id, dvc.9b$reg_conflict)
dvc.10b<-summarySE(sec.wave, measurevar="dv_rs", groupvars=c("treatment","confirmed_conflict"))
dvc.10b$.id<-2
dvc.10b$.idfact<-paste(dvc.10b$.id, dvc.10b$confirmed_conflict)
dvc.10b$treatment[dvc.10b$treatment==0] <- "Init, Lose"
dvc.10b$treatment[dvc.10b$treatment==1] <-"Target, Lose"
dvc.10b$treatment[dvc.10b$treatment==2] <-"Init, Win"
dvc.10b$treatment[dvc.10b$treatment==3] <-"Target, Win"
names(dvc.9b) <- c("treatment","confirmed_conflict","N", "dv_rs", "sd", "se", "ci", ".id", ".idfact")
dvc.11b<-rbind(dvc.9b,dvc.10b)
dvc.11b$.id <- factor(dvc.11b$.id)


figure.7.table<-xtable(dvc.11b)
figure.7.table$lo<-figure.7.table$dv_rs-figure.7.table$ci
figure.7.table$hi<-figure.7.table$dv_rs+figure.7.table$ci
caption.7<-c("Figure 7: Proximity to Conflict ")
figure.7.table<-xtable(figure.7.table[ , c(1,2,3,4,6,10,11)], caption.7)
digits(figure.7.table)[c(2, 3,4 )] <- 0

print(figure.7.table)#,include.rownames=FALSE)



postscript("figure_7_no_ethnic.eps")

ggplot(dvc.11b, aes(x=.idfact, y=dv_rs, fill=treatment )) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", # Use black outlines,
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin=dv_rs-ci, ymax=dv_rs+ci),
                size=.3,    # Thinner lines
                width=.1,
                position=position_dodge(.9)) +
  geom_text(aes(y=-.05,label=N), size=3, 
            position= position_dodge(width=0.9), 
            color="black") +
  xlab("Pre- and Post-War Responses") +
  ylab("Support for President") +
  scale_fill_grey(name="Survey", # Legend label, use darker colors
                  breaks=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Real War"),
                  labels=c("Init, Win", "Target, Win", "Init, Lose", "Target, Lose", "Actual War"))+
  scale_y_continuous(breaks=0:20*1) +
  coord_cartesian(ylim=c(-0.1,3)) +
  theme_bw()+
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ 
  theme(legend.justification=c(0,1), legend.position=c(0,1))+theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
  ) +
  #draws x and y axis line
  theme(axis.line = element_line(color = 'black'))+
  theme(legend.justification=c(0,1), legend.position=c(0,1))

dev.off()



